import json

from parameterized import parameterized
import unittest
import app
from api.login_api import LoginApi
from config import Logger

log = Logger(app.base_dir+'/log/all.log', level='info')
def build_data(path):
    list_data = []
    file_path = app.base_dir + "/data/login.json"
    with open(file_path, encoding="utf-8") as f:
        json_data = json.load(f)
        for data in json_data:
            login_data = data.get('login_data')
            status_code = data.get('status_code')
            success = data.get('success')
            code = data.get('code')
            message = data.get('message')
            list_data.append((login_data, status_code, success, code, message))
    return list_data


class Test_Login(unittest.TestCase):
    def setUp(self):
        self.login_api = LoginApi()
    @parameterized.expand(build_data("../data/login.json"))
    def test_login(self, login_data, status_code, success, code, message):
        response = self.login_api.login_api(login_data)
        log.logger.info(f"响应数据:{response.json()}")
        log.logger.info("响应状态码: {}".format(response.status_code))
        self.assertEqual(status_code, response.status_code)
        self.assertEqual(success, response.json()['success'])
        self.assertEqual(code, response.json()['code'])
        self.assertIn(message, response.json()['message'])
